// 一个跑质数的摸鱼脚本
let primeNumber = []
let arr = Array(0xffffff)
arr.fill(0)
arr = arr.map((v, i) => i)
arr = arr.filter((a) => a > 1)
let lastArrLength = arr.length
let startTime = new Date().getTime()
let timeline = new Date().getTime()
let totalTime = 0
let lastPrimeCount = 0
// logCount()
while (arr.length) {
  primeNumber.push(arr[0])
  arr = arr.filter((a) => a % arr[0])
  if (new Date().getTime() - timeline >= 1000) {
    totalTime++
    timeline = new Date().getTime()
    const cha = lastArrLength - arr.length
    const chaPrime = primeNumber.length - lastPrimeCount
    const totalMiao = lastArrLength / cha
    const totalS = lastArrLength / chaPrime
    lastPrimeCount = primeNumber.length
    lastArrLength = arr.length
    const rageMsg =
      cha == chaPrime
        ? `筛查速度${chaPrime}个/秒`
        : `筛查速度${cha}个/秒(全数字)${chaPrime}个/秒（质数）`
    const willTimeMsg =
      totalMiao == totalS
        ? `预计剩余时间：${formatTime(totalMiao)}`
        : `预计剩余时间：${formatTime(totalMiao)}（全数字）${formatTime(
            totalS
          )}（质数）`
    console.log(
      `剩余数字：${lastArrLength}；找到质数：${
        primeNumber.length
      }；${rageMsg}；${willTimeMsg};已运行时间：${formatTime(
        totalTime
      )}(循环) ${formatTime((timeline - startTime) / 1000)}（时钟）；`
    )
    //
  }
}
function formatTime(seconds) {
  const days = Math.floor(seconds / (24 * 3600))
  seconds %= 24 * 3600
  const hours = Math.floor(seconds / 3600)
  seconds %= 3600
  const minutes = Math.floor(seconds / 60)
  seconds %= 60

  let result = ''

  if (days > 0) {
    result += `${days}天 `
  }
  if (hours > 0 || days > 0) {
    // 如果有天数，小时可以显示
    result += `${hours}小时 `
  }
  if (minutes > 0 || hours > 0 || days > 0) {
    // 如果有小时或天数，分钟可以显示
    result += `${minutes}分 `
  }
  result += `${seconds.toFixed(2)}秒` // 秒始终显示

  return result.trim() // 去掉末尾的空格
}

console.log('完犊子了')
// 最快： 16007
// 最慢： 16007
// 上次： 16007
// function logCount() {
//   setTimeout(() => {
//     // lastArrLength
//     const cha = lastArrLength - arr.length
//     lastArrLength =arr.length
//     const totalMiao = lastArrLength/cha;
//     console.log(`剩余数字：${lastArrLength}找到质数：${primeNumber.length} 预计剩余时间：${totalMiao.toFixed(2)}秒`)
//     logCount();
//   }, 1000)
// }
